package com.ruiysoft.website.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ruiysoft.website.dto.CmsContentEx;
import com.ruiysoft.website.entity.CmsContent;
import com.ruiysoft.website.entity.CmsContentDetail;
import com.ruiysoft.website.mapper.CmsContentDetailMapper;
import com.ruiysoft.website.mapper.CmsContentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liudh on 2019/4/25.
 */

@Service
public class CmsContentService {
    @Autowired
    CmsContentMapper cmsContentMapper;

    @Autowired
    CmsContentDetailMapper cmsContentDetailMapper;

    public List<CmsContentEx> selectLeftJoinCatalog(Integer enterpriseId, String catalogCode) {
        return cmsContentMapper.selectLeftJoinCatalog(enterpriseId, catalogCode);
    }

    public List<CmsContentEx> selectList(String enterpriseId, String catalogCode) {
        EntityWrapper<CmsContent> ew = new EntityWrapper();
        List columns = new ArrayList();
        columns.add("createTime");
        columns.add("sequence");

        ew.eq("enterpriseId", enterpriseId);
        if(catalogCode != null && !catalogCode.equals("null") && !catalogCode.equals(""))
            ew.eq("catalogCode", catalogCode);

        ew.orderDesc(columns);

        // 为了转换到dto对象
        return JSON.parseObject(JSON.toJSONString(cmsContentMapper.selectList(ew)), new TypeReference<List<CmsContentEx>>(){});
    }

    public Object selectDetailByContentId(Integer id) {
        EntityWrapper<CmsContentDetail> ew = new EntityWrapper();
        ew.eq("contentId", id);
        List list = cmsContentDetailMapper.selectList(ew);
        if(list.size() > 0)
            return list.get(0);
        else
            return null;
    }

    public Object saveOrEdit(CmsContent cmsContent) {
        if(cmsContent.getId() != null) {
            cmsContent.setUpdateTime(new Date());
            return cmsContentMapper.updateById(cmsContent);
        }
        else {
            cmsContent.setCreateTime(new Date());
            return cmsContentMapper.insert(cmsContent);
        }
    }

    public Integer deleteById(Integer id) {
        return cmsContentMapper.deleteById(id);
    }

    public Object saveOrEdit4Detail(CmsContentDetail cmsContentDetail) {
        if(cmsContentDetail.getId() != null) {
            return cmsContentDetailMapper.updateById(cmsContentDetail);
        }
        else {
            CmsContent cmsContent = cmsContentMapper.selectById(cmsContentDetail.getId());
            if(cmsContent != null) {
                cmsContent.setChildren(1);
                cmsContent.setUpdateTime(new Date());
                cmsContentMapper.updateById(cmsContent);
            }
            return cmsContentDetailMapper.insert(cmsContentDetail);
        }
    }
}
